# 作者: 肖老师
# 2024年11月14日07时07分49秒
# xxxrlmk@163.com
class HouseItem:
    def __init__(self, name, area):
        self.name = name
        self.area = area

    def __str__(self):
        return f'{self.name} 占地 {self.area}'


class House:
    def __init__(self, house_type, area):
        self.house_type = house_type
        self.area = area
        self.free_area = area
        self.item_list = []

    def __str__(self):
        # Python 能够自动的将一对括号内部的代码连接在一起
        return ("户型：%s\n总面积：%.2f[剩余：%.2f]\n家具：%s"
                % (self.house_type, self.area,
                   self.free_area, self.item_list))

    # 注解方便pycharm进行联想，避免编程出错
    def add_item(self, item: HouseItem):
        # 判断剩余面积是否还可以放家具
        if self.free_area >= item.area:
            self.item_list.append(item.name)
            self.free_area -= item.area  # 剩余面积要减少
        else:
            print('空间不够，无法再放家具')


# 1. 创建家具
bed = HouseItem("席梦思", 4)
chest = HouseItem("衣柜", 2)
table = HouseItem("餐桌", 1.5)

print(bed)
print(chest)
print(table)
print('-' * 50)
house = House('二室一厅', 60)
print(house)
house.add_item(bed)
house.add_item(chest)
print(house)
